<template>
  <div class="goods-sku">
    <dl v-for="item in goods.specs" :key="item.id">
      <dt>{{ item.name }}</dt>
      <dd>
        <template v-for="val in item.values" :key="val.name">
          <!-- 图片类型规格 -->
          <img
            v-if="val.picture"
            :src="val.picture"
            :class="{ selected: val.selected, disabled: val.disabled }"
            @click="changeSelectedStatus(item, val)"
            :title="val.name"
          />
          <!-- 文字类型规格 -->
          <span
            v-else
            :class="{ selected: val.selected, disabled: val.disabled }"
            @click="changeSelectedStatus(item, val)"
          >
            {{ val.name }}
          </span>
        </template>
      </dd>
    </dl>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import axios from 'axios'
import powerSet from './powerSet'

const goods = ref([])
let pathMap = {}
const getGoods = async () => {
  const res = await axios.get(
    'http://pcapi-xiaotuxian-front-devtest.itheima.net/goods?id=1369155859933827074'
  )
  goods.value = res.data.result
  pathMap = getPathMap(goods.value)
  initDisabledStatus(goods.value.specs, pathMap)
}
getGoods()

//初始化禁用状态
const initDisabledStatus = (specs, pathMap) => {
  specs.forEach(spec => {
    spec.values.forEach(val => {
      if (pathMap[val.name]) {
        val.disabled = false
      } else {
        val.disabled = true
      }
    })
  })
}
//生成有效路径字典对象
const getPathMap = goods => {
  const pathMap = {}
  //effectiveSkus为有库存的sku组合
  const effectiveSkus = goods.skus.filter(item => item.inventory > 0)
  effectiveSkus.forEach(sku => {
    //selectedValArr表示有库存的规格值组成的数组，比如['黑色', '20cm', '中国']
    const selectedValArr = sku.specs.map(item => item.valueName)
    //valueArrPowerSet是子集，是个数组
    const valueArrPowerSet = powerSet(selectedValArr)
    valueArrPowerSet.forEach(item => {
      //item表示一个有效路径组成的数组，比如['黑色', '20cm']
      const path = item.join('-')
      if (pathMap[path]) {
        pathMap[path].push(sku.id)
      } else {
        pathMap[path] = [sku.id]
      }
    })
  })
  return pathMap
}

const changeSelectedStatus = (item, val) => {
  if (val.disabled) return
  if (val.selected) {
    val.selected = false
  } else {
    item.values.forEach(item => {
      item.selected = false
    })
    val.selected = true
  }
  updateDisabledStatus(goods.value.specs, pathMap)
  const index = getSelectedValues(goods.value.specs).findIndex(item => item === undefined)
  if (index > -1) {
    console.log('请选择完整')
  } else {
    const key = getSelectedValues(goods.value.specs).join('-')
    const skuObj = goods.value.skus.find(item => item.id === pathMap[key][0])
    console.log(skuObj)
  }
}
const getSelectedValues = specs => {
  const arr = []
  specs.forEach(spec => {
    const selectedVal = spec.values.find(val => val.selected)
    arr.push(selectedVal ? selectedVal.name : undefined)
  })
  return arr
}
const updateDisabledStatus = (specs, pathMap) => {
  specs.forEach((spec, index) => {
    const selectedValues = getSelectedValues(specs)
    spec.values.forEach(val => {
      selectedValues[index] = val.name
      const key = selectedValues.filter(item => item).join('-')
      if (pathMap[key]) {
        val.disabled = false
      } else {
        val.disabled = true
      }
    })
  })
}
</script>

<style lang="scss" scoped>
@mixin sku-state-mixin {
  border: 1px solid #e4e4e4;
  margin-right: 10px;
  cursor: pointer;

  &.selected {
    border-color: #27ba9b;
  }

  &.disabled {
    opacity: 0.6;
    border-style: dashed;
    cursor: not-allowed;
  }
}

.goods-sku {
  padding-left: 10px;
  padding-top: 20px;

  dl {
    display: flex;
    padding-bottom: 20px;
    align-items: center;

    dt {
      width: 50px;
      color: #999;
    }

    dd {
      flex: 1;
      color: #666;

      > img {
        width: 50px;
        height: 50px;
        margin-bottom: 4px;
        @include sku-state-mixin;
      }

      > span {
        display: inline-block;
        height: 30px;
        line-height: 28px;
        padding: 0 20px;
        margin-bottom: 4px;
        @include sku-state-mixin;
      }
    }
  }
}
</style>
